<?php

class MaterialStatisticController extends Controller {

	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules() {
		return array(
			array(
				'allow',
				'actions' => array(
					'viewProject',
					'viewProjectResult',
				),
				'roles' => array(
					'materialViewProject'
				),
			),
			array(
				'allow',
				'actions' => array(
					'projectMaterialAnalyze',
					'projectMaterialAnalyzeData',
				),
				'roles' => array(
					'projectMaterialAnalyze'
				),
			),
			array(
				'allow',
				'actions' => array(
					'viewPrice',
					'priceCheck',
				),
				'roles' => array(
					'materialPriceCheck'
				),
			),
			array(
				'deny',
				// deny all users
				'users' => array(
					'*'
				),
			),
		);
	}
	public function actions() {
		return array(
			'priceCheck' => array(
				'class' => 'web.controllers.materialStatistic.MaterialPriceCheckAction',
			),
			'projectMaterialAnalyzeData' => array(
				'class' => 'web.controllers.materialStatistic.ProjectMaterialAnalyzeDataAction',
			),
		);
	}

	public function actionViewPrice() {
		$this->render('viewPrice');
	}

	public function actionViewProject() {
		$searchModel = new MaterialStatisticForm('projectStatistic');
		$searchModel->unsetAttributes();
		if (isset($_GET['MaterialStatisticForm'])) {
			$searchModel->attributes = $_GET['MaterialStatisticForm'];
		}
		$this->render('viewProject', array(
				'statisticForm' => $searchModel,
			));
	}
	
	public function actionProjectMaterialAnalyze(){
		$this->render('projectMaterialAnalyze');
	}

	public function actionViewProjectResult() {
		$searchModel = new MaterialStatisticForm('projectStatistic');
		$searchModel->unsetAttributes();
		if (isset($_GET['MaterialStatisticForm'])) {
			$searchModel->attributes = $_GET['MaterialStatisticForm'];
		}
		if (!$searchModel->validate()) {
			echo '请选择项目及物品';
			return;
		}
		$result = array();
		$project = Project::model()->findByPk($searchModel->projectId);
		$materialModel = MaterialModel::model()->findByPk($searchModel->materialModelId);

		$requisitions = MaterialRequisitionItem::model()
			->findAll(
				array(
					'condition' => 'projectId=:projectId and materialModelId=:materialModelId',
					'params' => array(
						':projectId' => $searchModel->projectId,
						':materialModelId' => $searchModel->materialModelId,
					),
					'with' => array(
						'materialRequisition',
						'applicant'
					),
					'order' => 'materialRequisition.submitDate ASC'
				));
		foreach ($requisitions as $requisition) {
			$date = $requisition->materialRequisition->submitDate;
			if (!isset($result[$date])) {
				$result[$date] = array();
			}
			if (!isset($result[$date]['Requisition'])) {
				$result[$date]['Requisition'] = array();
			}
			$result[$date]['Requisition'][] = $requisition;
		}

		$items = MaterialWarehouseInboundItem::model()
			->findAll(
				array(
					'condition' => 'projectId=:projectId and materialModelId=:materialModelId',
					'params' => array(
						':projectId' => $searchModel->projectId,
						':materialModelId' => $searchModel->materialModelId,
					),
					'with' => array(
						'materialWarehouseInbound',
					),
					'order' => 'materialWarehouseInbound.submitDate ASC'
				));
		foreach ($items as $item) {
			$date = $item->materialWarehouseInbound->submitDate;
			if (!isset($result[$date])) {
				$result[$date] = array();
			}
			if (!isset($result[$date]['WarehouseInbound'])) {
				$result[$date]['WarehouseInbound'] = array();
			}
			$result[$date]['WarehouseInbound'][] = $item;
		}

		$items = MaterialReceiptItem::model()
			->findAll(
				array(
					'condition' => 'projectId=:projectId and materialModelId=:materialModelId',
					'params' => array(
						':projectId' => $searchModel->projectId,
						':materialModelId' => $searchModel->materialModelId,
					),
					'with' => array(
						'materialReceipt',
					),
					'order' => 'materialReceipt.receiptDate ASC'
				));
		foreach ($items as $item) {
			$date = $item->materialReceipt->receiptDate;
			if (!isset($result[$date])) {
				$result[$date] = array();
			}
			if (!isset($result[$date]['Receipt'])) {
				$result[$date]['Receipt'] = array();
			}
			$result[$date]['Receipt'][] = $item;
		}

		ksort($result);
		$this
			->renderPartial('_viewProjectResult',
				array(
					'result' => $result,
					'project' => $project,
					'materialModel' => $materialModel,
				));
	}

}
